package cn.dckeji.dcssm.controller.api;


import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.common.model.ResultPageModel;
import cn.dckeji.dcssm.common.utils.ListUtil;
import cn.dckeji.dcssm.dto.DemoDTO;
import cn.dckeji.dcssm.entity.Demo;
import cn.dckeji.dcssm.service.DemoService;
import cn.dckeji.dcssm.vo.DemoVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Kenny
 * @since 2019-02-21
 */
@RestController
@RequestMapping(value = "/api/demo",produces = "application/json;charset=UTF-8")
@Api(tags = "演示管理")
public class DemoApi {

    @Autowired
    private DemoService demoService;

    @GetMapping(value = "/page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "偏移量", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query")
    })
    ResultPageModel<DemoVO> page(@RequestParam Integer pageNumber, @RequestParam Integer pageSize) throws Exception {

        IPage<Demo> page = demoService.page(new Page<>(pageNumber, pageSize));
        List<Demo> records = page.getRecords();
        List<DemoVO> demoVOList = ListUtil.ListToList(records, DemoVO.class);
//        List<DemoVO> demoVOList = new ArrayList<>();
//        if (CollectionUtil.isNotEmpty(records)) {
//            for (Demo demo:records) {
//                DemoVO demoVO = new DemoVO();
//                BeanUtil.copyProperties(demo,demoVO);
//                demoVOList.add(demoVO);
//            }
//        }
        return ResultPageModel.page(page.getTotal(),demoVOList,"success");
    }

    @PostMapping("/insert")
    @ApiOperation("新增")
    ResultModel<Object> insert(@RequestBody DemoDTO demoDTO) {
        Demo demo = new Demo();
        //属性拷贝
        BeanUtil.copyProperties(demoDTO,demo);
        demo.setGmtCreate(LocalDateTime.now());

        demoService.save(demo);

        return ResultModel.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation("根据id删除")
    @ApiImplicitParam(name = "demoId", value = "demoId", required = true, paramType = "query")
    ResultModel<Object> deleteById(@RequestParam String demoId) {
        demoService.removeById(demoId);
        return ResultModel.success();
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    ResultModel<Object> update(@RequestBody Demo demo) {
        demo.setGmtModified(LocalDateTime.now());
        demoService.updateById(demo);
        return ResultModel.success();
    }


    @GetMapping("/testRedisInsert")
    @ApiOperation("测试redis插入")
    public ResultModel<Object> testRedisInsert() {
        Jedis jedis = RedisDS.create().getJedis();
        jedis.append("test", "123");
        return ResultModel.success();
    }

    @GetMapping("/testRedisGet")
    @ApiOperation("测试redis查询")
    @ApiImplicitParam(name = "key", value = "redis key", required = true, paramType = "query")
    public ResultModel<Object> testRedisGet(@RequestParam String key) {
        Jedis jedis = RedisDS.create().getJedis();
        String s = jedis.get(key);
        return ResultModel.success(s);
    }
}

